package routers

import (
	"fmt"
	"micro-bbs/middlewares"
	"micro-bbs/services"
	"micro-bbs/web/controllers"
	"net/http"
	"time"

	"github.com/gin-gonic/gin"
)

var (
	homeController 	*controllers.HomeController
	postController 	*controllers.PostController
	userController 	*controllers.UserController
	apiController 	*controllers.ApiController
	goodsController *controllers.GoodsController
	ssoController 	*controllers.SsoController
	captchaController  *controllers.CaptchaController
	thirdController  *controllers.ThirdController
)

// SetupRouter 配置路由信息
func SetupRouter() *gin.Engine {
	r := gin.Default()
	//r.LoadHTMLGlob("/views/**/*")
	// 路由组1 ，处理GET请求
	v1 := r.Group("/v1")

	//初始化控制器结构体
	initControllerStruct()
	//quizService := services.NewPostService()
	//obj := controllers.PostController{
	//	PostService: quizService,
	//}
	// {} 是书写规范
	{
		v1.GET("/home/postData",  homeController.PostData)
		v1.GET("/home/weekHotData",  homeController.WeekHotData)
		v1.GET("/home/list",  homeController.List)
		v1.GET("/discuss",  controllers.DiscussIndex)
		v1.GET("/discuss/list",  controllers.DiscussList)
		v1.GET("/post/list/:cate",  postController.List)
		v1.GET("/post/:id",  postController.Detail)
		v1.POST("/post/add",  postController.AddPost)
		v1.GET("/user/:id",  userController.Detail)
		v1.GET("/user/loginInfo",  userController.LoginInfo)
		v1.GET("/user/logout",  userController.Logout)
		v1.GET("/sso/token",  ssoController.Token)
		v1.GET("/login", login)
		v1.GET("submit", submit)
		v1.GET("/test", helloHandler)
	}

	//用户中心
	center := r.Group("/v2/members")
	center.Use(middlewares.JwtMiddleware())
	{
		//帖子相关
		center.POST("/post/add",  postController.AddPost)					//用户发帖

		//收益相关
		//center.GET("/consume", controller.JmfLiveConsume) //支出
		////球豆提现
		//center.POST("/beans/add", controller.JmfMemberBeansCashOut)              //球豆提现
		//center.GET("/beans/config", controller.JmfMemberBeansConfig)             //球豆配置
		//center.GET("/beans/open", controller.JmfMemberBeansOpen)                 //球豆是否开启
		//center.POST("/beans/open/setting", controller.JmfMemberBeansOpenSetting) //设置球豆开关
		//center.GET("/beans/ios/config", controller.JmfMemberIosBeansConfig)      //球豆配置-ios端
		//center.GET("/beans/list", controller.JmfMemberBeansCashOutList)          //球豆提现列表
	}

	v2 := r.Group("/v2")
	{
		v2.POST("/api/message/data", apiController.MessageData)
		v2.POST("/submit", submit)
		//验证码相关
		v2.GET("/captcha/generate",  captchaController.Generate)
		v2.GET("/captcha/image/:id",  captchaController.GetImage)
		//邮箱相关
		v2.GET("/email/sendCode",  thirdController.SendCode)
	}

	v3 := r.Group("/check")
	{
		v3.GET("/health", func(c *gin.Context) {
			//c.String(http.StatusOK, "hello word")
			c.JSON(http.StatusOK, gin.H{
				"code": 200,
				"data": []int{},
				"msg": "service is ok",
			})
		})
	}

	//grpc调用
	v4 := r.Group("/v4")
	{
		v4.GET("/goods/:id",  goodsController.Detail)
	}

	//r.Run(":8000")
	return r
}

//初始化所以控制器结构体
func initControllerStruct() {
	//首页控制器
	homeController = homeObj()
	//帖子控制器
	postController = postObj()
	//用户控制器
	userController = controllers.NewUserController()
	//api控制器
	apiController = controllers.NewApiController()
	//商品控制器 grpc调用
	goodsController = controllers.NewGoodsController()
	//验证码控制器
	captchaController = controllers.NewCaptchaController()
	//第三方控制器
	thirdController = controllers.NewThirdController()
}

//	首页控制器结构体
func homeObj() *controllers.HomeController {
	homeService := services.NewHomeService()
	obj := controllers.HomeController{
		HomeService: homeService,
	}
	return &obj
}

//	帖子控制器结构体
func postObj() *controllers.PostController {
	postService := services.NewPostService()
	obj := controllers.PostController{
		PostService: postService,
	}
	return &obj
}

func helloHandler(c *gin.Context) {
	time.Sleep(5*time.Second)
	c.JSON(http.StatusOK, gin.H{
		"code": 200,
		"message": "Hello www.100txy.com",
		"data": "test success",
	})
}

func login(c *gin.Context) {
	name := c.DefaultQuery("name", "jack")
	c.String(200, fmt.Sprintf("hello %s\n", name))
}

func submit(c *gin.Context) {
	name := c.DefaultQuery("name", "lily")
	c.String(200, fmt.Sprintf("hello %s\n", name))
}

